﻿//
// *************************************************************************************
// WP-Framework
// 
// Developed by
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//


using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace KretschIT.WP_Fx.EasyMoq.Generator
{
    public class AssemblyResolver : IAssemblyResolver
    {
        public Assembly GetAssembly(string name, IEnumerable<Assembly> assemblies)
        {
            if (this.CannotResolveAssebly(name, assemblies))
            {
                return null;
            }
            foreach (var a in assemblies)
            {
                if (this.MatchNames(name, a.FullName))
                {
                    return a;
                }
            }
            return null;
        }

        private bool CannotResolveAssebly(string name, IEnumerable<Assembly> assemblies)
        {
            return string.IsNullOrWhiteSpace(name) || assemblies == null;
        }

        private bool MatchNames(string name, string assemblyFullName)
        {
            string assemblyName = this.ExtractName(assemblyFullName);
            return name == assemblyName;
        }

        private string ExtractName(string assemblyFullName)
        {
            var parts = assemblyFullName.Split(',');
            return parts.First();
        }
    }
}
